﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**
 * 通过接口优化委托===
 */
namespace P19_6 {
    internal class Program {
        static void Main(string[] args) {
            IProductFactory pizzFactory = new PizzFactory();
            IProductFactory toyCarFactory = new ToyCarFactory();

            WrapFactory wrapFactory = new WrapFactory();

           Box box1 = wrapFactory.WrapProduct(pizzFactory);
           Box box2 = wrapFactory.WrapProduct(toyCarFactory);
            Console.WriteLine(box1.Prod.Name);
            Console.WriteLine(box2.Prod.Name);
            Console.ReadLine();
        }
    }

    class Logger {
        public void Log(Product prod) {
            Console.WriteLine("Product '{0}' created {1}. Price is {2}.", prod.Name, DateTime.UtcNow, prod.Price);
        }
    }
    class Product {
        public string Name { get; set; }
        public double Price { get; set; }
    }
    class Box {
        public Product Prod { get; set; }
    }
    class WrapFactory {
        public Box WrapProduct(IProductFactory productFactory) {
            Box box = new Box();
            Product product = productFactory.Make();
            box.Prod = product;
            return box;
        }
    }

    class ProductFactory {
        public Product MakePizza() {
            Product product = new Product();
            product.Name = "Pizza";
            product.Price = 12;
            return product;
        }

        public Product MakeToyCar() {
            Product prod = new Product();
            prod.Name = "Toy Car";
            prod.Price = 100;
            return prod;
        }
    }


    interface IProductFactory {
        Product Make();
    }

    //实现接口
    class PizzFactory : IProductFactory {
        public Product Make() {
            Product product = new Product();
            product.Name = "Pizza";
            product.Price = 12;
            return product;
        }
    }

    class ToyCarFactory : IProductFactory {
        public Product Make() {
            Product prod = new Product();
            prod.Name = "Toy Car";
            prod.Price = 100;
            return prod;
        }
    }
}
